package com.share.lottery.view;


import com.share.lottery.R;

import android.content.Context;
import android.graphics.Color;

import android.os.Handler;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author py
 *自定义Toast
 */
public class CustomToast {

	private static Toast mToast;
	private static Handler mHandler = new Handler();
	private static TextView mTextView;
	private static Runnable r = new Runnable() {
		public void run() {
			mToast.cancel();
		}
	};

	public static void makeText(Context mContext, String text, int duration) {

		mHandler.removeCallbacks(r);
		if (mToast != null){
			mTextView.setText(text);
			mToast.setDuration(duration);
		}
		else{
			//mToast = Toast.makeText(mContext, text, duration);
			mToast = new Toast(mContext);
			imageToast(mContext, text);
			mToast.setDuration(duration);
		}
		mHandler.postDelayed(r, 1000);

		mToast.show();
	}

	public static void makeText(Context mContext, int resId, int duration) {
		makeText(mContext, mContext.getResources().getString(resId), duration);
	}
	
	private static void imageToast(Context context,String msg){
    	mTextView = new TextView(context);  
        
        // 设置“背景布局”  
        	LinearLayout mLinearLayout = new LinearLayout(context);  
        	mLinearLayout.setBackgroundResource(R.drawable.toast);//toast_frame就是系统的toast的背景图片  
            mTextView.setText(msg); 
            mTextView.setTextColor(Color.WHITE);
            mTextView.setGravity(Gravity.CENTER);//设置文字居中  
            mTextView.setTextSize(16);
            mTextView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);  
            mLinearLayout.addView(mTextView);  
            mToast.setView(mLinearLayout); 
    } 
	
}